<style>
.modal_historybox {
	padding: 20px;
}
.modal_history_top {
	padding: 10px 0;
	border-bottom: 1px solid #CCC;
	margin-bottom: 10px
}
ul.mhlist {
	background: #EEE;
	height: 400px;
	overflow-y: scroll;
}
ul.mhlist li {
	padding: 10px;
	cursor: pointer;
	border-bottom: 1px solid #CCC;
}
ul.mhlist li:hover, ul.mhlist li.isselect {
	background: #009688;
	color: #FFF;
}
ul.mhlist li h3 {
	font-size: 20px;
	float: left;
	line-height: 30px;
	width: 100px;
	margin-right: 5px;
}
</style>
<div class="modal_history" style="display:none">
  <div class="modal_historybox">
    <div class="modal_history_top">
      <form action="" method="post" class="layui-form" onsubmit="return false;" enctype="multipart/form-data">
        <div class="secrchinput" style="margin-top:0">
          <button type="submit" onclick="funExtend_memberList(1)"><i class="fa fa-search"></i></button>
          <input type="text" name="modal_history_txtTopupSeachStr" id="modal_history_txtTopupSeachStr" placeholder="输入单号">
        </div>
        <div class="layui-btn-group">
          <button class="layui-btn layui-btn-primary layui-btn-small" day="1">今天</button>
          <button class="layui-btn layui-btn-primary layui-btn-small" day="2">昨天</button>
          <button class="layui-btn layui-btn-primary layui-btn-small" day="3">最近3天</button>
          <button class="layui-btn layui-btn-primary layui-btn-small" day="4">最近7天</button>
          <button class="layui-btn layui-btn-primary layui-btn-small" day="5">本月</button>
        </div>
      </form>
    </div>
    <div>
      <div style="width:120px; float:left;">
        <ul class="mhlist">
        </ul>
      </div>
      <div style="width:600px; float:right;">
        <div class="layui-form" style="margin-bottom:10px;">
          <table class="layui-table">
            <tbody>
              <tr>
                <td><b>总金额</b> <span id="mmh_orderfee">0</span>
                  <input type="hidden" id="mmh_id" value="0"/></td>
                <td><b>优惠</b> <span id="mmh_disountfee">0</span></td>
                <td><b>合计</b> <span id="mmh_total">0</span></td>
                <td width="40%"><span id="mmh_status"></span></td>
              </tr>
            </tbody>
          </table>
        </div>
        <div class="layui-form" style="height:290px; overflow-y:scroll">
          <table class="layui-table" lay-skin="line">
            <thead>
              <tr>
                <th><input type="checkbox" lay-skin="primary" lay-filter="checkall"></th>
                <th>序号</th>
                <th>品名</th>
                <th>数量</th>
                <th>单价</th>
                <th>小计</th>
                <th>操作</th>
              </tr>
            </thead>
            <tbody id="mhgoodslist">
            </tbody>
          </table>
        </div>
      </div>
    </div>
    <div class="text-right" style="clear:left;">
      <button class="layui-btn modal_historyboxprint1">打印</button>
      <button class="layui-btn" onclick="funExtend_refundreason(0,'')">整单退款</button>
      <button class="layui-btn layui-btn-primary modal_historyboxclosebtn">关闭</button>
    </div>
  </div>
</div>
<style>
.modal_tuikuan,modal_tuikuanreason{padding:20px;}
.modal_tuikuanreason{ text-align:center; padding:10px;}
.modal_tuikuanreason span{ border:1px solid #CCC; display:inline-block; padding:5px; min-width:60px; margin:5px; text-align:center; border-radius:4px; cursor:pointer}
.modal_tuikuanreason span.isselect{ border-color:#90b82d; background:#90b82d; color:#FFF;}
</style>
<div class="modal_tuikuan" style="display:none">
  <div class="modal_tuikuanybox">
    <div class="">
      <form action="" method="post" id="modal_tuikuanform" class="layui-form" onsubmit="return false;" enctype="multipart/form-data">
		  <div class="layui-form-item">
          <label class="layui-form-label">退款类型</label>
          <div class="layui-input-block">
            <input type="text" class="layui-input" readonly id="mtk_type" value="商品退款" />
          </div>
        </div>
        <div class="layui-form-item mtk_good">
          <label class="layui-form-label">商品名称</label>
          <div class="layui-input-block">
            <input type="text" class="layui-input" readonly id="mtk_goodname" />
            <input type="hidden" class="layui-input" id="mtk_id" />
          </div>
        </div>
        <div class="layui-form-item">
          <label class="layui-form-label">退款原因</label>
          <div class="layui-input-block">
            <select id="mtk_reason" lay-ignore class="form-control">
				{if !$shop['refundreason']}
              <option value="客户原因">客户原因</option>
				{else}
				{php $refundreasonAry=explode('|',$shop['refundreason']);}
				{loop $refundreasonAry $row} 
				<option value="{$row}">{$row}</option>
				{/loop}
				{/if}
            </select>
          </div>
        </div>
        <div class="layui-form-item mtk_good">
          <label class="layui-form-label">数量</label>
          <div class="layui-input-block">
            <input type="number" min="1" price="" max="3" class="form-control" id="mtk_num"/>
          </div>
        </div>
		  <div class="layui-form-item mtk_good">
          <label class="layui-form-label">商品回收</label>
          <div class="layui-input-block">
			  <input type="radio" name="mtk_getback" value="0" title="不回收">
              <input type="radio" name="mtk_getback" value="1" title="回收" checked>
          </div>
        </div>
        <div class="layui-form-item">
          <label class="layui-form-label">金额</label>
          <div class="layui-input-block">
            <input type="text" class="layui-input" readonly id="mtk_fee"/>
          </div>
        </div>
        <div class="layui-form-item">
          <label class="layui-form-label">付款方式</label>
          <div class="layui-input-block">
            <select id="mtk_paytype" lay-ignore class="form-control"> 
              <option value="2">现金</option>
			  <option value="99">原路返回</option> 
            </select> 
          </div>
        </div>
		  <div class="layui-form-item">
          <label class="layui-form-label">备注</label>
          <div class="layui-input-block">
            <input type="text" class="layui-input" id="mtk_remark" />
          </div>
        </div>
        <div class="text-right">
          <button type="button" class="layui-btn mtk_btnsubmit">确认退款</button>
          <button type="button" class="layui-btn layui-btn-primary mtk_btnclose">关闭</button>
        </div>
      </form>
    </div>
  </div>
</div>
<script language="javascript">
function funExtend_history(){
	layer.open({
		type: 1,
		title:0,
		area:"790px",
		skin: 'layui-layer-rim',
		content:$(".modal_history").show(),
		success: function(layero, index){
			layui.use(['form'], function(){
				var form = layui.form;
				form.render();
				form.on('checkbox(checkall)', function(data){
					$(".modal_historybox .layui-table input:checkbox").prop("checked",data.elem.checked);
					form.render("checkbox");
				});
			});
			$(".modal_historybox .layui-btn-group .layui-btn").bind("click",function(){
				$(this).removeClass("layui-btn-primary").siblings().addClass("layui-btn-primary");
				$(this).addClass("isselect").siblings().removeClass("isselect");
				funExtend_historyGetorderlist();
			});
			$(".modal_historybox .layui-btn-group .layui-btn").eq(0).click();
			$(".modal_historyboxclosebtn").bind("click",function(){
				layer.close(index);
			});
			$(".modal_historyboxprint1").bind("click",function(){
				if($("#mmh_id").val()=="0")return;
				fun_print(1,$("#mmh_id").val());
			});
		},
		end:function(){
			$(".modal_history").hide();
			$(".modal_historyboxclosebtn").unbind("click");
			$(".modal_historyboxprint1").unbind("click");
		}
	});
}
function funExtend_historyGetorderlist(){
	var datetype=$(".modal_historybox .layui-btn-group .isselect").size() ? $(".modal_historybox .layui-btn-group .isselect").attr("day"):0 ;
	var keyword=$("#modal_history_txtTopupSeachStr").val();
	var index2=layer.load(1, {shade: [0.1,'#fff']});
	$.post("{php echo $this->createMobileUrl('index',array('op'=>'getorderlist'))}",{datetype:datetype,keyword:keyword},function(returnstr){
		layer.close(index2);
		console.log(returnstr);
		$(".mhlist").empty();
		
		var result=eval("("+returnstr+")");
		if(!result.success){
			layer.msg(result.msg,{icon:2,time:1000});
			return ;
		}
		var list=result.list;
		for(var i in list){
			var temp='<li oid="'+list[i].id+'" onclick="funExtend_historyselect('+list[i].id+')"><h3>'+list[i].serialnum+'</h3><div><small>'+list[i].out_trade_no+'</small></div><div>'+fromatTime(list[i].createtime)+'</div></li>';
			$(".mhlist").append(temp);
		}
	});
}
function funExtend_historyselect(id){
	$(".mhlist li[oid='"+id+"']").addClass("isselect").siblings().removeClass("isselect");
	var index2=layer.load(1, {shade: [0.1,'#fff']});
	$.post("{php echo $this->createMobileUrl('index',array('op'=>'gethistoryview'))}",{id:id},function(returnstr){
		layer.close(index2);
		console.log(returnstr);
		$("#mhgoodslist").empty();
		var result=eval("("+returnstr+")");
		if(!result.success){
			layer.msg(result.msg,{icon:2,time:1000});
			return ;
		}
		var trade=result.trade;
		$("#mmh_id").val(trade.id);
		$("#mmh_orderfee").text(fee2num(trade.order_fee));
		$("#mmh_disountfee").text(fee2num(trade.discount_fee)+"("+trade.discount+")");
		
		$("#mmh_total").text(fee2num(parseInt(trade.total_fee)));
		var tmp='';
		if(parseInt(trade.status)==3){
			tmp='<span class="label label-danger">部分退款</span> ';
		}else if(parseInt(trade.status)==4){
			tmp='<span class="label label-danger">已退款</span> '; 
		} else{
		   tmp+=parseInt(trade.ispay) ? '<span class="label label-success">已支付-'+trade.paytypestr+'</span> ' : '<span class="label label-default">未付款</span> ';
			if(parseInt(trade.status)==0){
				tmp+='<span class="label label-default">挂单</span>';
			}else if(parseInt(trade.status)==1){
				tmp+='<span class="label label-info">已下单</span>';
			}else if(parseInt(trade.status)==2){
				tmp+='<span class="label label-info">完成</span>';
			}
		}
		$("#mmh_status").html(tmp);
		var goods=result.goods;
		for(var i=0;i<goods.length;i++){
			var temp='<tr mid="'+goods[i].id+'"><td><input type="checkbox" name="mid['+goods[i].id+']" value="'+goods[i].id+'" lay-skin="primary"></td><td>'+(i+1)+'</td><td class="mtrtitle">'+goods[i].title+'</td><td class="mtrnum">'+goods[i].num+'</td><td class="mtrprice">'+goods[i].price+'</td><td>'+fee2num(goods[i].total_fee)+'</td><td>';
			if(parseInt(goods[i].ispay) && parseInt(goods[i].num) && parseInt(goods[i].total_fee)){
				temp+='<a href="javascript:funExtend_refundreason(1,'+goods[i].id+')">退款</a>';
			}
			temp+='</td></tr>';
			$("#mhgoodslist").append(temp);
		}
		layui.use(['form'], function(){
			var form = layui.form;
			form.render();
		});
	});
}
function funExtend_refundreason(type,id){
	var title=$(".modal_tuikuanreasonbox .isselect").text();
	if(type==1){
		funExtend_historyTuikuan(id,title);
	}else{
		funExtend_historyTuikuan(0);
	}
}
function funExtend_historyTuikuan(goodid){
	$("#modal_tuikuanform")[0].reset();
	var refundertype=parseInt(goodid) ? 1: 0;
	var formtitle=refundertype ? "商品退款":"订单退款";
	if(refundertype){
		$("#mtk_id").val(goodid);
		$(".mtk_good").show();
		$("#mtk_goodname").val($("#mhgoodslist tr[mid='"+goodid+"'] .mtrtitle").text());
		var target=$("#mhgoodslist tr[mid='"+goodid+"'] .mtrnum");
		$("#mtk_num").attr("max",$("#mhgoodslist tr[mid='"+goodid+"'] .mtrnum").text());
		$("#mtk_num").attr("price",$("#mhgoodslist tr[mid='"+goodid+"'] .mtrprice").text());
		$("#mtk_num").val("1");
		$("#mtk_fee").attr("readonly",true).val($("#mhgoodslist tr[mid='"+goodid+"'] .mtrprice").text());
	}else{
		$("#mtk_id").val("0");
		$(".mtk_good").hide();
		$("#mtk_goodname").val('');
		$("#mtk_fee").attr("readonly",true).val($("#mmh_total").text());
	}
	var remark=$("#mtk_reason").val();
	layer.open({
		type: 1,
		title:formtitle,
		area:"400px",
		skin: 'layui-layer-rim',
		content:$(".modal_tuikuan").show(),
		success: function(layero, index){
			$("#mtk_num").bind("change",function(){
				if(!refundertype)return;
				var tempval=parseInt($(this).val());
				var maxnum=parseInt($(this).attr("max"));
				var minnum=parseInt($(this).attr("min"));
				if(tempval>maxnum)$(this).val(maxnum);
				if(tempval<minnum)$(this).val(minnum);
				tempval=parseInt($(this).val());
				var price=parseInt(parseFloat($("#mtk_num").attr("price"))*100);
				var fee=fee2num(price*tempval);
				$("#mtk_fee").val(fee);
			});
			$(".mtk_btnclose").bind("click",function(){
				layer.close(index);
			});
			
			$(".mtk_btnsubmit").bind("click",function(){
				layer.confirm('是否确认退款？', {btn:['确认退款', '关闭']},function(){
					var goodsid=$("#mtk_id").val();
					var orderid=$("#mmh_id").val();
					var reason=$("#mtk_reason").val();
					var paytype=$("#mtk_paytype").val();
					var num=parseInt($("#mtk_num").val());
					var fee=parseFloat($("#mtk_fee").val());
					var maxfee=parseFloat($("#mtk_fee").attr("fee"));
					var isgetback=$(".modal_tuikuan .layui-form-radioed").prev().val();
					if(fee>num*maxfee && goodsid){
						layer.msg("退款金额不能大于付款金额",{icon:2,time:1800});
						return;
					}
					if(num<=0 && goodsid){
						layer.msg("已退款",{icon:2,time:1800});
						return;
					}
					var index3=layer.load(1, {shade: [0.1,'#fff']});
					$.post("{php echo $this->createMobileUrl('index',array('op'=>'refundorder'))}",{id:goodsid,num:num,fee:fee,remark:remark,orderid:orderid,reason:reason,paytype:paytype,isgetback:isgetback},function(returnstr2){
						layer.close(index3);
						console.log(returnstr2);
						var result2=eval("("+returnstr2+")");
						if(result2.success){
							fun_print(5,result2.refundid);
							layer.msg("退款成功",{icon:1,time:1500});
							funExtend_historyselect(orderid);
						}else{
							layer.msg(result2.msg,{icon:2,time:1500});
							return ;
						}
					});
				});
			});
			
		},
		end:function(){
			$(".modal_tuikuan").hide();
			$("#mtk_num").unbind("change");
			$(".mtk_btnclose").unbind("click");
			$(".mtk_btnsubmit").unbind("click");
		}
	});
}

</script>